"""
本模块包括一些demo程序。
"""
from os import system
import os
import sys
from time import sleep

from pist import PistonCluster
from algor import *

delay_time = 0.02

def perform_visible_action(pst : PistonCluster, inst : List[int], time: float = delay_time):
    print(pst.getState())
    print("".join("^" if k in inst else " " for k in pst.getValidRange()))
    pst.performInstructions(inst)

    sleep(delay_time)
    sys.stdout.write("\033[0;0H")

def bst_demo(N: int):
    system("clear")

    pst = PistonCluster().set(N)

    
    arr = extension(N)
    for i in arr:
        print("performing Extension")
        perform_visible_action(pst,i)

    system("clear")
    print("mid")
    print(pst.getState())
    sleep(1)

    stps = 0
    system("clear")
    arr = substract(N)
    for i in arr:
        print("performing Subtraction")
        perform_visible_action(pst,i)
        stps += 1

    print("final")
    print(pst.getState())
    print("Compat.\n Taking {} steps".format(stps))


def bstbk_demo(N: int):
    system("clear")

    pst = PistonCluster().set(N)

    
    arr = extension(N)
    for i in arr:
        print("performing Extension")
        perform_visible_action(pst,i)


    system("clear")
    print("mid")
    print(pst.getState())
    sleep(1)
    
    stps = 0
    system("clear")
    arr = substractbk(N)
    for i in arr:
        print("performing Subtraction")
        perform_visible_action(pst,i)
        stps += 1

   
    print("final")
    print(pst.getState())
    print("Compat.\n Taking {} steps".format(stps))




def chs_demo(N: int):
    system("clear")

    pst = PistonCluster().set(N)
    
    
    arr = impulse(N,1)

    for i in arr:
        print("performing Extension")
        perform_visible_action(pst,i)


    system("clear")
    print("mid")
    print(pst.getState())
    sleep(1)

    system("clear")
    arr = impulse(N, -1, N)
    stps = 0
    for i in arr:
        print("performing Subtraction")
        perform_visible_action(pst,i)
        stps += 1


        if pst.isCompat():
            print("final")
            print(pst.getState())
            print("Compat.\n Taking {} steps".format(stps))
            break


def glph_demo(N: int):
    psto = PistonCluster()
    for i in range(1,N+1):
        psto.set_l(i)
        pls = impulse(i,-1,i)
        stps = 0
        for j in pls:
            psto.performInstructions(j)
            stps += 1
            if psto.isCompat():
                print("{},{}".format(i,stps))
                break
        